﻿Imports PdfSharp
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf.IO
Imports System.IO
Imports System
Imports System.Web

Module modulo_pdfsharp_watermark

    Public tempfile As String

    Sub watermarks(ByVal QUANTITA As String, ByVal filename As String, ByVal drawstate As String)

        Dim watermark As String = ""

        If drawstate = "modifica" Then
            watermark = "Da modificare"
        ElseIf drawstate = "provvisori" Then
            watermark = "Modificati"
        ElseIf drawstate = "definitivi" Then
            watermark = ""
        End If
        Try

            'copio il file originale 
            File.Copy(filename, Application.StartupPath & "\temp.pdf", True)

            'seleziono il file da aprire
            Dim document As PdfDocument = PdfReader.Open(Application.StartupPath & "\temp.pdf")
            'Dim document As PdfDocument = Open(Application.StartupPath & "\temp.pdf")

            Dim emSize As Integer = 100
            'scelgo il font
            Dim font As New XFont("Arial Black", emSize, XFontStyle.BoldItalic)

            Dim gfx As XGraphics
            Dim i As Integer
            Dim page As PdfPage
            Dim size As XSize
            Dim format As XStringFormat
            Dim brush As XBrush


            If (document.Version < 14) Then
                document.Version = 14
            End If

            For i = 0 To document.Pages.Count - 1
                page = document.Pages(i)

                gfx = XGraphics.FromPdfPage(document.Pages(i))

                'Prendo la grandezza (in point) del testo
                size = gfx.MeasureString(watermark, font)

                ' Spostiamo al centro , trasformiamo e ruotiamo
                gfx.TranslateTransform(page.Width.Point / 2, page.Height.Point / 2)
                gfx.RotateTransform(-Math.Atan(page.Height.Point / page.Width.Point) * 180 / Math.PI)
                gfx.TranslateTransform(-page.Width.Point / 2 + 50, -page.Height.Point / 2 + 100)

                'Creiamo  la string format
                format = New XStringFormat()
                format.Alignment = XStringAlignment.Near
                format.LineAlignment = XLineAlignment.Near

                ' Creiamo un colore rosso 
                brush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))

                ' Scriviamo il testo
                gfx.DrawString(watermark, font, brush, New XPoint((page.Width.Point - size.Width) / 2, (page.Height.Point - size.Height) / 2))

               
            Next
            ' Salvo

            document.Save(Application.StartupPath & "\temp.pdf")
            tempfile = Application.StartupPath & "\temp.pdf"
        Catch ex As PdfSharpException
            tempfile = filename
            MsgBox("Devi risalvare il pdf tramite il tasto modifica pdf")
        Catch ex1 As Exception
            MsgBox("Devi risalvare il pdf tramite il tasto modifica pdf")
        End Try
        ' ...e apro il file
        'Process.Start(Application.StartupPath & "\prova1.pdf")
    End Sub

    Function dati_stampa(ByVal cliente As String, ByVal odp As String, ByVal numero As String, ByVal colore As String, Optional ByVal quantita As String = "") As Object

        'copio il file originale 
        tempfile = Application.StartupPath & "\temp.pdf"
        File.Copy(tempfile, Application.StartupPath & "\temp1.pdf", True)
        Try
            'seleziono il file da aprire
            Dim document As PdfDocument = PdfReader.Open(Application.StartupPath & "\temp1.pdf")

            Dim emSize As Integer = htesto
            'scelgo il font
            Dim font As New XFont("Arial Black", emSize, XFontStyle.Regular)

            Dim gfx As XGraphics
            Dim i As Integer
            Dim page As PdfPage
            'Dim size As XSize
            Dim format As XStringFormat
            Dim brush As XBrush


            If (document.Version < 14) Then
                document.Version = 14
            End If

            For i = 0 To document.Pages.Count - 1
                page = document.Pages(i)

                gfx = XGraphics.FromPdfPage(document.Pages(i))


                If i = 0 Then
                    ' Spostiamo al centro , trasformiamo e ruotiamo
                    'gfx.TranslateTransform(page.Width.Point / 2, page.Height.Point / 2)

                    'Creiamo  la string format
                    format = New XStringFormat()
                    format.Alignment = XStringAlignment.Near
                    format.LineAlignment = XLineAlignment.Near

                    ' Creiamo un colore rosso 
                    brush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))

                    If cliente <> Nothing Then
                        gfx.DrawString(cliente, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posizionex), page.Height.Millimeter - (page.Height.Millimeter - posizioney)))
                    End If
                    If odp <> Nothing Then
                        gfx.DrawString("N. ORV: " & odp, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posizionex), (page.Height.Millimeter - (page.Height.Millimeter - posizioney)) + htesto))

                    End If
                    If numero <> Nothing Then
                        gfx.DrawString("Q.tà: " & numero, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posizionex), (page.Height.Millimeter - (page.Height.Millimeter - posizioney)) + htesto * 2))
                    End If
                    If colore <> Nothing Then
                        gfx.DrawString("RAL: " & colore, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posizionex), (page.Height.Millimeter - (page.Height.Millimeter - posizioney)) + htesto * 3))
                    End If

                    If quantita = "" Or quantita = "0" Then
                    Else
                        brush = New XSolidBrush(XColor.FromArgb(128, 0, 0, 255))
                        font = New XFont("Arial Black", 35, XFontStyle.Regular)
                        gfx.DrawString(quantita & " A MAG.", font, brush, New XPoint(page.Width.Millimeter - (185), (page.Height.Millimeter - (270)) + 35))

                    End If

                End If


            Next
            ' Salvo

            document.Save(Application.StartupPath & "\temp1.pdf")
            tempfile = Application.StartupPath & "\temp1.pdf"
        Catch ex As PdfSharpException
            tempfile = tempfile
            MsgBox("Devi risalvare il pdf tramite il tasto modifica pdf")
        Catch ex1 As Exception
            MsgBox("Devi risalvare il pdf tramite il tasto modifica pdf")
        End Try

        Return tempfile
    End Function

   

    Public Function Stampa_dati_produzione(ByVal fileorigine As String, ByVal posx As Integer, ByVal posy As Integer, ByVal htesto As Integer, ByVal tipologia As String, _
                                            Optional ByVal cliente As String = "", _
                                            Optional ByVal ordine As String = "", _
                                            Optional ByVal quantita As String = "", _
                                            Optional ByVal colore As String = "")

        Dim filetemporaneo As String = "pdf" & tipologia & ".pdf"
        If filetemporaneo.Contains("/") Then
            filetemporaneo = filetemporaneo.Replace("/", "_")
        End If
        Dim percorsofiletemporaneo As String = ""



        File.Copy(fileorigine, Application.StartupPath & "\" & filetemporaneo, True)
        Try
            'seleziono il file da aprire
            Dim document As PdfDocument = PdfReader.Open(Application.StartupPath & "\" & filetemporaneo)


            'Dim emSize As Integer = htesto
            'scelgo il font
            Dim font As New XFont("Arial Black", htesto, XFontStyle.Regular)

            Dim gfx As XGraphics
            Dim i As Integer
            Dim page As PdfPage
            'Dim size As XSize
            Dim format As XStringFormat
            Dim brush As XBrush


            If (document.Version < 14) Then
                document.Version = 14
            End If

            For i = 0 To document.Pages.Count - 1
                page = document.Pages(i)

                gfx = XGraphics.FromPdfPage(document.Pages(i))



                If i = 0 Then
                    ' Spostiamo al centro , trasformiamo e ruotiamo
                    'gfx.TranslateTransform(page.Width.Point / 2, page.Height.Point / 2)


                    'Creiamo  la string format
                    format = New XStringFormat()
                    format.Alignment = XStringAlignment.Near
                    format.LineAlignment = XLineAlignment.Near

                    ' Creiamo un colore rosso 
                    brush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
                    'posizionex = FrmDisegniClienti.Tbposx.Text
                    'posizioney = FrmDisegniClienti.Tbposy.Text

                    ' Scriviamo il testo
                    If cliente <> Nothing Then
                        gfx.DrawString(cliente, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posx), page.Height.Millimeter - (page.Height.Millimeter - posy)))
                    End If
                    If ordine <> Nothing Then
                        gfx.DrawString("N. ORV: " & ordine, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posx), (page.Height.Millimeter - (page.Height.Millimeter - posy)) + htesto))

                    End If
                    If quantita <> Nothing Then
                        gfx.DrawString("Q.tà: " & quantita, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posx), (page.Height.Millimeter - (page.Height.Millimeter - posy)) + htesto * 2))
                    End If
                    If colore <> Nothing Then
                        gfx.DrawString("RAL: " & colore, font, brush, New XPoint(page.Width.Millimeter - (page.Width.Millimeter - posx), (page.Height.Millimeter - (page.Height.Millimeter - posy)) + htesto * 3))
                    End If
                End If

            Next
            ' Salvo

            document.Save(Application.StartupPath & "\" & filetemporaneo)
            percorsofiletemporaneo = Application.StartupPath & "\" & filetemporaneo
        Catch ex As PdfSharpException
            percorsofiletemporaneo = fileorigine

            If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\pdf editor 2\pdfedit.exe") = True Then
                Dim risultato = MsgBox("Devi risalvare il PDF con PDF EDITOR, Vuoi modificarlo ora?", MsgBoxStyle.YesNo)
                If risultato = MsgBoxResult.Yes Then
                    Dim processo = Process.Start(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\pdf editor 2\pdfedit.exe", percorsofiletemporaneo)
                    While Not processo.HasExited
                        Continue While
                    End While
                End If

            End If

        End Try

        Return percorsofiletemporaneo
    End Function

    Public Sub DIVIDI_PDF_FRONTE(ByVal FILESORGENTE As String)
        Dim CARTELLA As String
        CARTELLA = My.Computer.FileSystem.GetParentPath(FILESORGENTE)

        Dim document As PdfDocument = PdfReader.Open(FILESORGENTE, PdfDocumentOpenMode.Import)


        For i = 0 To document.Pages.Count - 1
            Dim newdoc As New PdfDocument
            Dim page As PdfPage
            'Dim gfx As XGraphics
            page = document.Pages(i)
            'gfx = XGraphics.FromPdfPage(document.Pages(i))

            newdoc.AddPage(page)
            newdoc.Save(CARTELLA & "\" & "PAG" & i & ".pdf")

        Next
        MsgBox("DOCUMENTO SUDDIVISO")
    End Sub

    Public Sub DIVIDI_PDF_FRONTE_RETRO(ByVal FILESORGENTE As String)
        Dim CARTELLA As String
        CARTELLA = My.Computer.FileSystem.GetParentPath(FILESORGENTE)

        Dim document As PdfDocument = PdfReader.Open(FILESORGENTE, PdfDocumentOpenMode.Import)

        For i = 0 To document.Pages.Count - 1
            Dim newdoc As New PdfDocument
            Dim page As PdfPage
            Dim page1 As PdfPage
            If page Is Nothing Then
                page = document.Pages(i)
                GoTo prosegui
            End If
            If page.Elements.Count <> Nothing And page1 Is Nothing Then
                page1 = document.Pages(i)
                newdoc.AddPage(page)
                newdoc.AddPage(page1)
                newdoc.Save(CARTELLA & "\" & "PAGG" & i & ".pdf")
                page = Nothing
                page1 = Nothing
            End If
prosegui:

        Next
        MsgBox("DOCUMENTO SUDDIVISO")
    End Sub


    Function VERIFICA_VERSIONE(ByVal NOME_FILE As String) As String
        Dim FILE As String = ""
        Try
            Dim document As New PdfDocument
            document = PdfReader.Open(NOME_FILE)
            If (document.Version < 14) Then
                FILE = ""
            ElseIf (document.Version > 14) Then
                FILE = NOME_FILE
            End If
        Catch EX As PdfSharp.PdfSharpException
            FILE = NOME_FILE
        End Try
        Return FILE
    End Function

End Module


